package com.lineengine.lineengine.context;


import cn.hutool.json.JSONUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 业务上下文默认实现
 * </p >
 *
 * @author wenrong.yu
 * @since 2020-11-10 09:38
 */
public abstract class AbstractContext<T extends IContext<T>> implements IContext<T> {
    private final Map<String, Object> nodeCostTimeMonitorMap = new ConcurrentHashMap<>();

    private boolean isRollback = false;

    @Override
    public void recordCostTime(String nodeName, Long costTime) {
        this.nodeCostTimeMonitorMap.put(nodeName, costTime);
    }

    @Override
    public String printCostTime() {
        return JSONUtil.toJsonPrettyStr(this.nodeCostTimeMonitorMap);
    }

    @Override
    public boolean isRollback() {
        return this.isRollback;
    }

    @Override
    public void markRollback() {
        this.isRollback = true;
    }
}
